package com.huilian.iotbox.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huilian.iotbox.admin.dao.LogLoginDao;
import com.huilian.iotbox.admin.service.LogLoginService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.LogLoginDto;
import com.huilian.iotbox.data.po.LogLogin;
import com.huilian.iotbox.data.vo.LogLoginVo;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LogLoginServiceImpl implements LogLoginService {

    @Resource
    private LogLoginDao logLoginDao;

    @Override
    public int insert(LogLogin logLogin) {
        return logLoginDao.insert(logLogin);
    }

    @Override
    public ServerResponse findListByKeyword(UserInfo userInfo, LogLoginDto logLoginDto) {
        PageHelper.startPage(logLoginDto.getPageNum(), logLoginDto.getPageSize(),false);
        List<LogLoginVo> logLoginList = logLoginDao.findAllByKeyword(logLoginDto);
        PageInfo pagResult = new PageInfo(logLoginList);
        if (logLoginList.size() < 10) {
            pagResult.setTotal(logLoginDto.getPageSize() * logLoginDto.getPageNum());
        } else {
            pagResult.setTotal(logLoginDto.getPageSize() * logLoginDto.getPageNum() + 1);
        }
        return ServerResponse.createBySuccess(pagResult);
    }

    @Override
    public ServerResponse delete(Integer id) {
        logLoginDao.delete(id);
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse batchDelete(LogLoginDto logLoginDto) {
        logLoginDao.batchDelete(logLoginDto.getBatchDeleteToList());
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse deleteInStartAndEndDateTime(LogLoginDto logLoginDto) {
        logLoginDao.deleteInStartAndEndDateTime(logLoginDto);
        return ServerResponse.createBySuccess();
    }
}
